/**
 * Плагин диалога bootstrap
 * @author Vladimir Mozolev (mutantdd@yandex.ru)
 */
(function($){
    
    var localData = {};
    
    $.dialog = function(id, inObj){
        
        var options = {
            title: "",
            body: "",
            buttons:{}
        };
        
        var command = "open";
        if (typeof inObj == "string") {
            command = inObj || "open";
        } else if (typeof inObj == "object" && inObj != null) {
            $.extend(options, inObj);
            localData[id] = options;
        } else {
            throw "wrong data type!";
        }
        
        switch (command) {
            case "open":
                $("#" + id).remove();
                var content = '<div id="' + id +'" class="modal hide fade">\
                <div class="modal-header">\
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>\
                    <h3>' + options.title + '</h3>\
                </div>\
                <div class="modal-body">\
                    <p>' + options.body + '</p>\
                </div>\
                <div class="modal-footer">';
                for (var i in options.buttons) {
                    var active = '';
                    if (options.active_button == i) {
                        active = ' btn-primary';
                    }
                    content += '<a href="#" class="btn' + active + '">' + i + '</a>';
                }

                content += '</div></div>';
                $('body').append(content);

                $.each($('#' + id + ' div.modal-footer > a'), function(indexInArray, valueOfElement) {
                    $(valueOfElement).click(options.buttons[$(valueOfElement).text()]);
                });
                $("#" + id).modal('show');
                break;
                
            case "close":
                $("#" + id).modal('hide');
                break;
        }

    };

})(jQuery);